package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.bean.OrderBean;
import com.example.myapplication.utils.ImageLoader;

import java.util.List;

public class PrivateOrderAdapter extends RecyclerView.Adapter<PrivateOrderAdapter.OrderViewHolder> {
    private List<OrderBean> orderList;

    public PrivateOrderAdapter(List<OrderBean> orderList) {
        this.orderList = orderList;
    }

    public PrivateOrderAdapter() {

    }
    // 设置订单列表并刷新适配器
    public void setOrderList(List<OrderBean> orderList) {
        this.orderList = orderList;
        notifyDataSetChanged(); // 刷新适配器
    }

    public static class OrderViewHolder extends RecyclerView.ViewHolder {
        ImageView shopImageView;
        TextView shopNameTextView;
        ImageView dishImageView;
        TextView dishNameTextView;
        TextView orderPriceTextView;

        public OrderViewHolder(View itemView) {
            super(itemView);
            // 初始化视图
            shopImageView = itemView.findViewById(R.id.iv_shop_image);
            shopNameTextView = itemView.findViewById(R.id.tv_shop_name);
            dishImageView = itemView.findViewById(R.id.iv_dish_image);
            dishNameTextView = itemView.findViewById(R.id.tv_dish_name);
            orderPriceTextView = itemView.findViewById(R.id.tv_order_price);
        }
    }

    @NonNull
    @Override
    public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建 ViewHolder
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item_layout, parent, false);
        return new OrderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderViewHolder holder, int position) {
        // 绑定数据
        OrderBean order = orderList.get(position);

        // 设置店铺信息
        loadImage(holder.itemView.getContext(), order.getShopImage(), holder.shopImageView);

        holder.shopNameTextView.setText(order.getShopName());

        // 设置菜品信息
        loadImage(holder.itemView.getContext(), order.getFoodImage(), holder.dishImageView);

        holder.dishNameTextView.setText(order.getFoodName());

        // 设置订单总价
        holder.orderPriceTextView.setText("￥" + String.valueOf(order.getTotalPrice()));
    }

    // 使用ImageLoader加载图片
    private void loadImage(Context context, String imagePath, ImageView imageView) {
        ImageLoader.load(context, imagePath, imageView, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
    }


    @Override
    public int getItemCount() {
        return orderList.size();
    }
}
